package com.drinks.yiniang.ui.activity.community;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.community.CommunityDetailAdapter;
import com.drinks.yiniang.adapter.community.CommunityDetailGoodsAdapter;
import com.drinks.yiniang.common.recycler.decoration.ItemSpacesDecoration;
import com.drinks.yiniang.common.recycler.decoration.RecommendGoodsItemDecoration;
import com.drinks.yiniang.entity.community.CommunityDetailJson;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.goods.RecommendGoodsJson;
import com.drinks.yiniang.io.api.CommunityApiIO;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.io.api.ShoppingCartApiIO;
import com.drinks.yiniang.ui.activity.goods.GoodsDetailsActivity;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.entity.BaseJson;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 社区动态详情页面
 *
 * @author han
 * @date 2019/09/10
 */
public class CommunityDetailActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {

    //nav
    private NavigationBarLayout nav_bar_community_detail;
    private BaseRecyclerView rcv_community_detail;
    //header view
    //相关的商品
    private BaseRecyclerView rcv_community_detail_goods;
    //标题
    private TextView tv_community_detail_title;
    //作者
    private TextView tv_community_detail_author;
    //正文
    private TextView tv_community_detail_content;

    //详情adapter
    private CommunityDetailAdapter mCommunityDetailAdapter;
    //相关商品adapter
    private CommunityDetailGoodsAdapter mCommunityDetailGoodsAdapter;

    //社区id
    private String mCommunityId;

    @Override
    protected void initView() {
        nav_bar_community_detail = findViewById(R.id.nav_bar_community_detail);

        rcv_community_detail = findViewById(R.id.rcv_community_detail);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        mCommunityId = getIntent() != null ? getIntent().getStringExtra(Constants.KeyCommunityId) : null;

        initAdapter();
        initGoodsAdapter();
        getCommunityDetail();
        getRecommend();
    }

    private void initAdapter() {
        rcv_community_detail.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 2));
        rcv_community_detail.addItemDecoration(RecommendGoodsItemDecoration.getItemDPDecoration(10));

        mCommunityDetailAdapter = new CommunityDetailAdapter(mContext, new ArrayList<GoodsJson.GoodsList>());
        mCommunityDetailAdapter.addHeaderView(getHeaderView());
        mCommunityDetailAdapter.setHeaderAndEmpty(true);
        mCommunityDetailAdapter.setCenterEmpty(getLayoutInflater(), rcv_community_detail);
        mCommunityDetailAdapter.bindToRecyclerView(rcv_community_detail);
    }

    private void initGoodsAdapter() {
        rcv_community_detail_goods.setLayoutManager(RecyclerUtils.getHorizontalLinearLayout(mContext));
        rcv_community_detail_goods.addItemDecoration(ItemSpacesDecoration.getLRTItemCountDPDecoration(10));

        mCommunityDetailGoodsAdapter = new CommunityDetailGoodsAdapter(mContext, new ArrayList<CommunityDetailJson.CommunityDetailGoods>());
        mCommunityDetailGoodsAdapter.bindToRecyclerView(rcv_community_detail_goods);
    }

    //获取header
    private View getHeaderView() {
        View mHeaderView = getLayoutInflater().inflate(R.layout.header_community_detail, (ViewGroup) rcv_community_detail.getParent(), false);

        rcv_community_detail_goods = mHeaderView.findViewById(R.id.rcv_community_detail_goods);

        tv_community_detail_title = mHeaderView.findViewById(R.id.tv_community_detail_title);
        tv_community_detail_author = mHeaderView.findViewById(R.id.tv_community_detail_author);
        tv_community_detail_content = mHeaderView.findViewById(R.id.tv_community_detail_content);

        return mHeaderView;
    }

    @Override
    protected void initListener() {
        nav_bar_community_detail.setOnNavgationBarClickListener(this);

        mCommunityDetailAdapter.setOnItemClickListener(this);
        mCommunityDetailAdapter.setOnItemChildClickListener(this);
        mCommunityDetailGoodsAdapter.setOnItemClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_community_detail;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent mIntent;

        if (adapter instanceof CommunityDetailAdapter) {
            GoodsJson.GoodsList data = mCommunityDetailAdapter.getItem(position);

            if (data == null) {
                return;
            }

            mIntent = new Intent(mContext, GoodsDetailsActivity.class);
            mIntent.putExtra(Constants.KeyGoodsId, data.getId());
            startActivity(mIntent);
        } else if (adapter instanceof CommunityDetailGoodsAdapter) {
            Intent intent = new Intent(mContext, CommunityGoodsActivity.class);
            intent.putExtra(Constants.KeyObject, (Serializable) mCommunityDetailGoodsAdapter.getData());
            startActivity(intent);
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof CommunityDetailAdapter) {
            GoodsJson.GoodsList data = mCommunityDetailAdapter.getItem(position);

            if (data != null && view.getId() == R.id.iv_home_goods_add_cart)  addToCart(data.getId());
        }
    }

    //获取社区详情
    private void getCommunityDetail() {
        if (StringUtils.isEmpty(mCommunityId)) {
            ToastUtils.showShort(R.string.text_community_not_exist);
            return;
        }

        showLoadingDialog();

        CommunityApiIO.getInstance().getCommunityDetail(mCommunityId, new APIRequestCallback<CommunityDetailJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CommunityDetailJson res) {
                setData(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setData(CommunityDetailJson.CommunityDetailData data) {
        tv_community_detail_title.setText(data.getTitle());
        tv_community_detail_author.setText("作者：" + StringUtils.getNoNullString(data.getUsername()));
        tv_community_detail_content.setText(Html.fromHtml(data.getContent()));

        //加载商品列表
        if (CommonUtils.isEmptyList(data.getGoods())) {
            rcv_community_detail_goods.setVisibility(View.GONE);
            return;
        }

        rcv_community_detail_goods.setVisibility(View.VISIBLE);

        mCommunityDetailGoodsAdapter.getData().clear();
        mCommunityDetailGoodsAdapter.addData(data.getGoods());
        mCommunityDetailGoodsAdapter.loadMoreEnd();
    }

    private void addToCart(String goodsId) {
        showLoadingDialog();

        ShoppingCartApiIO.getInstance().addToShopCart(goodsId, new APIRequestCallback<BaseJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(BaseJson res) {
                ToastUtils.showShort(getString(R.string.text_add_success));
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取推荐商品
    private void getRecommend() {
        GoodsApiIO.getInstance().getRecommendGoodsList(new APIRequestCallback<RecommendGoodsJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(RecommendGoodsJson res) {
                if (mCommunityDetailAdapter == null) {
                    return;
                }

                mCommunityDetailAdapter.getData().clear();
                mCommunityDetailAdapter.addData(res.getData());
                mCommunityDetailAdapter.loadMoreEnd();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                mCommunityDetailAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
